from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_rag.entity.do.doc_do import RagDoc
from module_rag.entity.do.rag_do import Rag
from module_rag.entity.vo.doc_vo import DocModel, DocPageQueryModel
from utils.page_util import PageUtil
from module_admin.entity.do.user_do import SysUser

class DocDao:
    """
    知识库附属文档信息模块数据库操作层
    """

    @classmethod
    async def get_doc_detail_by_id(cls, db: AsyncSession, doc_id: int):
        """
        根据文档标题获取知识库附属文档信息详细信息

        :param db: orm对象
        :param doc_id: 文档标题
        :return: 知识库附属文档信息信息对象
        """
        doc_info = (
            (
                await db.execute(
                    select(RagDoc)
                    .where(
                        RagDoc.doc_id == doc_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return doc_info

    @classmethod
    async def get_doc_detail_by_info(cls, db: AsyncSession, doc: DocModel):
        """
        根据知识库附属文档信息参数获取知识库附属文档信息信息

        :param db: orm对象
        :param doc: 知识库附属文档信息参数对象
        :return: 知识库附属文档信息信息对象
        """
        doc_info = (
            (
                await db.execute(
                    select(RagDoc).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return doc_info

    @classmethod
    async def get_doc_list(cls, db: AsyncSession, query_object: DocPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取知识库附属文档信息列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 知识库附属文档信息列表信息对象
        """
        query = (
            select(RagDoc,Rag,SysUser)
            .where(
                # RagDoc.rag_id == query_object.rag_id if query_object.rag_id else True,
                # RagDoc.user_id == query_object.user_id if query_object.user_id else True,
                RagDoc.doc_name.like(f'%{query_object.doc_name}%') if query_object.doc_name else True,
                RagDoc.type == query_object.type if query_object.type else True,
                RagDoc.author == query_object.author if query_object.author else True,
                RagDoc.remote_url == query_object.remote_url if query_object.remote_url else True,
                RagDoc.serect_level == query_object.serect_level if query_object.serect_level else True,
                RagDoc.status == query_object.status if query_object.status else True,
                RagDoc.create_date == query_object.create_date if query_object.create_date else True,
            )
            .join(SysUser, (SysUser.user_id == RagDoc.user_id))
            .join(Rag, (Rag.id == RagDoc.rag_id))
            .order_by(RagDoc.doc_id)
            .distinct()
        )
        doc_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return doc_list

    @classmethod
    async def add_doc_dao(cls, db: AsyncSession, doc: DocModel):
        """
        新增知识库附属文档信息数据库操作

        :param db: orm对象
        :param doc: 知识库附属文档信息对象
        :return:
        """
        db_doc = RagDoc(**doc.model_dump(exclude={}))
        db.add(db_doc)
        await db.flush()

        return db_doc

    @classmethod
    async def edit_doc_dao(cls, db: AsyncSession, doc: dict):
        """
        编辑知识库附属文档信息数据库操作

        :param db: orm对象
        :param doc: 需要更新的知识库附属文档信息字典
        :return:
        """
        await db.execute(update(RagDoc), [doc])

    @classmethod
    async def delete_doc_dao(cls, db: AsyncSession, doc: DocModel):
        """
        删除知识库附属文档信息数据库操作

        :param db: orm对象
        :param doc: 知识库附属文档信息对象
        :return:
        """
        await db.execute(delete(RagDoc).where(RagDoc.doc_id.in_([doc.doc_id])))

